<?php

App::uses('AppController', 'Controller');

class AdminController extends AppController {
	
	public $layout = "admin";
	
	public $uses = array('User');
	
	public $components = array(
		'Auth' => array(
			'loginAction' => array(
				'controller' => 'admin',
				'action' => 'login'
			),
			'loginRedirect' => '/admin',
			'logoutRedirect' => '/admin',
		)
	);
	
	public function index() {
		$this->loadModel('Setting');
        
        if (! empty($this->data)) {
            $data = Set::extract('Setting.{n}', $this->data);
            foreach($data as &$row) {
                $row = array('Setting' => $row);
            }
            
            if ($this->Setting->saveAll($data)) {
                $this->setFlash('Podešavanja su sačuvana.', 'ok');
            } else {
                $this->setFlash('Podešavanja nisu sačuvana.');
            }
            
            $this->redirect('index');
        }
        
        $this->set('data', $this->Setting->find('all'));
	}
	
	public function login() {
		if($this->request->isPost()) {
			
			if($this->Auth->login()) {
				$this->redirect($this->Auth->redirect());
			}else{
				$this->setFlash('Prijava nije uspela. Pokušajte ponovo.', 'error');
			}
		}
	}
	
	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	
}